
import {
  ParamsType,
  ProColumns,
  ProTable,
  ProTableProps,
} from '@ant-design/pro-components';
import { getFilter, getSorter } from './util';

type ProTablePlusProps<T, U, ValueType = 'text'> = {
  /** 默认的Column */
  columns?: ProColumns<T, ValueType>[];
  /** 查询列表接口 */
  // eslint-disable-next-line no-unused-vars
  query: (params: any) => Promise<any>;
  // eslint-disable-next-line no-unused-vars
  request: (params: any, sorter: any, filter: any) => Promise<any>;
  /** 模块名称 */
  moduleName: string;
  /** 是否显示索引列 */
  showIndexColumn?: boolean;
  /** 搜索框提示内容 */
  searchPlaceholder?: string;
  /** 表格的Y轴高度 */
  scroll?: any;
} & Partial<ProTableProps<T, U, ValueType>>;

/**
 * ProTable
 * @param props
 * @returns
 */
function ProMaxTable<T extends Record<string, any>, U extends ParamsType, ValueType = 'text'>(
  props: ProTablePlusProps<T, U, ValueType>,
) {

  const { query, searchPlaceholder, columns, request, scroll } = props;

  return (
    <>
      <ProTable<T, U, ValueType>
        headerTitle={'查询表格'}
        cardProps={{
          style: { height: "calc(100vh - 160px)" }
        }}
        rowKey="id"
        search={false}
        // @ts-ignore
        options={{
          search: {
            placeholder: searchPlaceholder || '关健字搜索',
          },
          // setting: false,
          fullScreen: true,
          reload: request || query
        }}
        {...props}
        pagination={{ style: {} }}
        toolBarRender={

          (action, rows) => [
            ...(props.toolBarRender ? props.toolBarRender(action, rows) : []),

          ]}
        // @ts-ignore
        params={{
          ...props.params,
        }}
        request={async (params, sorter, filter) => {
          if (request) {
            return request(params, sorter, filter);
          }
          if (query) {
            const res = await query({ ...params, ...getSorter(sorter, 'a'), ...getFilter(filter) });
            return {
              data: res.data?.items || [],
              success: res.success,
              total: res.data?.totalItems || 0,
            };
          }
          if (props.dataSource) {
            return {
              data: props.dataSource,
              success: true,
              total: props.dataSource.length,
            };
          }
          return {}

        }}
        scroll={scroll && scroll.x ? {
          x: scroll.x,
          y: scroll.y || window.innerHeight - 340,
        } : {
          x: 1000,
          y: scroll && scroll.y ? scroll.y : window.innerHeight - 340,

        }}
        columns={columns}
      />
    </>
  );
}

export default ProMaxTable;